package com.fw.application.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fw.application.controller.base.UserController;
import com.fw.common.Builder;
import com.fw.common.IdXD;
import com.fw.mes.Result;
import com.fw.system.web.model.entity.TbIntegralLog;
import com.fw.system.web.model.entity.TbSignUser;
import com.fw.system.web.model.entity.TbSysRule;
import com.fw.system.web.model.entity.TbUser;
import com.fw.system.web.model.vo.SignVo;
import com.fw.system.web.service.ITbIntegralLogService;
import com.fw.system.web.service.ITbSignUserService;
import com.fw.system.web.service.ITbSysRuleService;
import com.fw.system.web.service.ITbUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import static com.fw.mes.ResultUtils.success;

/**
 * <p>
 * 签到表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-03-28
 */
@RestController
@RequestMapping("/web/sign")
@Api(tags = "签到相关")
public class TbSignUserController implements UserController {


    @Autowired
    private ITbSignUserService signUserService;
    @Autowired
    private ITbUserService userService;
    @Autowired
    private ITbSysRuleService ruleService;
    @Autowired
    private IdXD idXD;
    @Autowired
    private ITbIntegralLogService logService;


    @GetMapping("/checkSign")
    @ApiOperation("检验今天是否签到 为空带表没签到弹出签到页面")
    public Result checkSign() {
        TbSysRule startAndEndTime = ruleService.getById(8);
        if (LocalDateTime.now().compareTo(startAndEndTime.getStartTime()) < 0)
            return new Result().fail(500,"活动未开始");
        if (LocalDateTime.now().compareTo(startAndEndTime.getEndTime()) > 0) {
            return new Result().fail(500,"活动已结束");
        }
        String userId = this.getUser().getId();
        //验证当前用户今天是否签到
        TbSignUser daysDate = signUserService.getOne(Wrappers.<TbSignUser>lambdaQuery().eq(TbSignUser::getUserId, userId)
                .last("and to_days(create_time) = to_days(now())"));
        if (daysDate != null)
            return success();
        List<TbSignUser> list = signUserService.list(Wrappers.<TbSignUser>lambdaQuery().eq(TbSignUser::getUserId, userId)
                .eq(TbSignUser::getSignOk, 0));
        return success(list.size());
    }

    @GetMapping("/signUser")
    @ApiOperation("用户签到")
    public Result signUser() {

        TbUser u = this.getUser();
        TbSysRule sysRule = ruleService.getById(11);
        SignVo signVo = new SignVo();
        int i = 0;
        Builder.of(TbSignUser::new)
                .with(TbSignUser::setId, idXD.nextId())
                .with(TbSignUser::setCreateTime, LocalDateTime.now())
                .with(TbSignUser::setSignOk, 0)
                .with(TbSignUser::setSignTime, LocalDate.now())
                .with(TbSignUser::setUserId, u.getId()).build().insert();
        //验证累计满不满七天
        List<TbSignUser> list = signUserService.list(Wrappers.<TbSignUser>lambdaQuery().eq(TbSignUser::getUserId, u.getId())
                .eq(TbSignUser::getSignOk, 0));
        signVo.setDayNum(list.size());
        if (list.size() >= 7) {
            list.forEach(item -> {
                item.setSignOk(1).updateById();
            });
            //增加双倍积分
            i = Integer.parseInt(sysRule.getRuleValue()) * 2;
            u.setIntegral(u.getIntegral() + i).updateById();

            signVo.setSignNum(i);
            return success(signVo);
        } else {
            i = Integer.parseInt(sysRule.getRuleValue());
            u.setIntegral(u.getIntegral() + i).updateById();
        }
        signVo.setSignNum(Integer.valueOf(sysRule.getRuleValue()));

        logService.addIntegral(Builder
                .of(TbIntegralLog::new).with(TbIntegralLog::setIntegralName, sysRule.getRuleKey())
                .with(TbIntegralLog::setUserId, u.getId())
                .with(TbIntegralLog::setIntegralNum, i)
                .with(TbIntegralLog::setIsType, 1).build());
        return success(signVo);
    }
}

